package com.yhthu.bean;

import com.yhthu.utils.TextJudger;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class Record {

    private String name;
    private String birthday;
    private String phone;
    private Map<Integer, String> donationRecipient;
    private String address;
    private String postcode;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getBirthday() {
        return birthday;
    }

    public void setBirthday(String birthday) {
        this.birthday = birthday;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public Map<Integer, String> getDonationRecipient() {
        return donationRecipient;
    }

    public void setDonationRecipient(Map<Integer, String> donationRecipient) {
        this.donationRecipient = donationRecipient;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getPostcode() {
        return postcode;
    }

    public void setPostcode(String postcode) {
        this.postcode = postcode;
    }

    /**
     * 根据属性Map构建Record对象
     *
     * @return
     */
    public static Record getRecordByAttributesMap(Map<String, String> attributesMap) {
        Record record = new Record();
        record.setDonationRecipient(new HashMap<>());
        for (String key : attributesMap.keySet()) {
            if (key.equals("donation") || key.equals("recipient")) {
                String[] donationValue = attributesMap.get("donation").split(",+");
                String[] recipientValue = attributesMap.get("recipient").split(",+");
                for (int i = 0; i < Math.min(donationValue.length, recipientValue.length); i++) {
                    if (TextJudger.isDigit(donationValue[i].trim())) {
                        record.getDonationRecipient().put(Integer.valueOf(donationValue[i].trim()), recipientValue[i]);
                    }
                }
            } else {
                try {
                    Field field = record.getClass().getDeclaredField(key);
                    field.setAccessible(true);
                    field.set(record, attributesMap.get(key));
                } catch (NoSuchFieldException | IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
        return record;
    }


    /**
     * 检查Record的各个字段是否合法
     *
     * @return
     */
    public boolean isValidRecord() {
        // 检查姓名和生日
        if (name == null || TextJudger.hasDigit(name)) {
            System.out.println(this + ": 姓名不合法");
            return false;
        }
        if (birthday == null) {
            System.out.println(this + ": 生日不合法");
            return false;
        }

        // 检查地址
        if (!(address.endsWith("NSW") || address.endsWith("VIC"))) {
            System.out.println(this + ": 地址格式不合法");
            return false;
        }

        // 检查邮编
        if (!(TextJudger.isDigit(postcode) & postcode.length() == 4)) {
            System.out.println(this + ": 邮编格式不合法");
            return false;
        }

        // 检查电话
        if (!(TextJudger.isDigit(phone) && phone.length() == 8)) {
            System.out.println(this + ": 电话格式不合法");
            return false;
        }

        return true;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Record record = (Record) o;
        return name.equals(record.name) &&
                birthday.equals(record.birthday);
    }

    @Override
    public int hashCode() {
        return Objects.hash(name, birthday);
    }

    @Override
    public String toString() {
        return "Record{" +
                "name='" + name + '\'' +
                ", birthday='" + birthday + '\'' +
                ", phone='" + phone + '\'' +
                ", donationRecipient=" + donationRecipient +
                ", address='" + address + '\'' +
                ", postcode='" + postcode + '\'' +
                '}';
    }
}
